<?php
namespace App\Logic\Tool;
use App\Models\Amazon\DailyFbaInventory;
use App\Models\Basics\Account;
use App\Models\Basics\ProductClassify;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductSku;
use App\Models\SalesReport\VolumeFile;
class DailyCal
{
    public static function exe(){
        try{
            ini_set('memory_limit', '200M');
            set_time_limit(0);
            $days = getDayDiffByDateStr('2021-01-01','2021-06-29');
            $path = VolumeFile::getFilePath('2021-01-01','2021-06-29',1);
            $fp = fopen($path, 'a');
            fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
            $head = self::getHead('2021-01-01',$days);
            fputcsv($fp, $head);
            $pas = ProductAmazonSku::query()->select('country_id','psku_id')->distinct()->get();
            $i = 0;
            $num = 800;
            $pSku = [];
            $classify = [];
            while(true) {
                if ($pas->isEmpty()) break;
                $_pas = $pas->splice($i, $num);
                foreach ($_pas as $pa) {
                    $item = [];
                    $item['country'] = Account::$_country[$pa->country_id];
                    $item['area'] = Account::$_area[Account::$_countryArea[$pa->country_id]];
                    if(!array_key_exists($pa->psku_id,$pSku)){
                        $ps = ProductSku::getModelById($pa->psku_id);
                        $pSku[$pa->psku_id]['psku_code'] = $ps->psku_code;
                        $pSku[$pa->psku_id]['psku_cn'] = $ps->psku_cn;
                        $pSku[$pa->psku_id]['child_classify_id'] = $ps->child_classify_id;
                    }
                    $item['psku_code'] = $pSku[$pa->psku_id]['psku_code'];
                    $item['psku_cn'] = $pSku[$pa->psku_id]['psku_cn'];
                    if(!array_key_exists($pSku[$pa->psku_id]['child_classify_id'],$classify)){
                        $pc = ProductClassify::getTitleById($pSku[$pa->psku_id]['child_classify_id']);
                        $classify[$pSku[$pa->psku_id]['child_classify_id']] = $pc;
                    }
                    $item['classify'] = $classify[$pSku[$pa->psku_id]['child_classify_id']];
                    for($i = 0;$i <= $days;$i++){
                        $date = date("Y-m-d",strtotime("+{$i} days",strtotime('2021-01-01')));
                        $quantity = DailyFbaInventory::query()
                            ->where('country_id',$pa->country_id)
                            ->where('psku_id',$pa->psku_id)
                            ->where('date_at','>=',$date.' 00:00:00')
                            ->where('date_at','<=',$date.' 23:59:59')
                            ->sum('quantity');
                        $item[$date] = $quantity;
                    }
                    fputcsv($fp, $item);
                }
            }
            $flag = fclose($fp);
            if($flag){
                VolumeFile::addFile('2021-01-01','2021-06-29',1,basename($path),1);
                VolumeFile::mailForCreateFile(1,$path);
            }
        }catch (\Exception $e){
            report($e);
        }
    }

    protected static function getHead($start,$days){
        $title1 = ['国家','站点','SKU','产品名称','品类'];
        $title2 = [];
        $_start = strtotime($start);
        for($i = 0;$i <= $days;$i++){
            $title2[] = date("Y.m.d",strtotime("+{$i} days",$_start));
        }
        return array_merge($title1,$title2);
    }
}
